Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
precisionAccelerometer.h
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, 2016, NXP Semiconductors N.V.,
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of NXP Semiconductors N.V. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL NXP SEMICONDUCTORS N.V. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 /*! \file precisionAccelerometer.h
27  \brief Implements accelerometer calibration routines
28 */
29 
30 #ifndef PRECISIONACCELEROMETER_H
31 #define PRECISIONACCELEROMETER_H
32 
33 /// calibration constants
34 #define ACCEL_CAL_AVERAGING_SECS 2 ///< calibration measurement averaging period (s)
35 #define MAX_ACCEL_CAL_ORIENTATIONS 12 ///< number of stored precision accelerometer measurements
36 
37 /// accelerometer measurement buffer
38 typedef struct AccelBuffer
39 {
40  float fGsStored[MAX_ACCEL_CAL_ORIENTATIONS][3]; ///< uncalibrated accelerometer measurements (g)
41  float fSumGs[3]; ///< averaging sum for current storage location
42  int16_t iStoreCounter; ///< number of remaining iterations at FUSION_HZ to average measurement
43  int16_t iStoreLocation; ///< -1 for none, 0 to 11 for the 12 storage locations
44  int16_t iStoreFlags; ///< denotes which measurements are present
45 } AccelBuffer;
46 
47 /// precision accelerometer calibration structure
48 typedef struct AccelCalibration
49 {
50  // start of elements stored in flash memory
51  float fV[3]; ///< offset vector (g)
52  float finvW[3][3]; ///< inverse gain matrix
53  float fR0[3][3]; ///< forward rotation matrix for measurement 0
54  // end of elements stored in flash memory
55  float fmatA[10][10]; ///< scratch 10x10 matrix used by calibration algorithms
56  float fmatB[10][10]; ///< scratch 10x10 matrix used by calibration algorithms
57  float fvecA[10]; ///< scratch 10x1 vector used by calibration algorithms
58  float fvecB[4]; ///< scratch 4x1 vector used by calibration algorithms
59  float fA[3][3]; ///< ellipsoid matrix A
60  float finvA[3][3]; ///< inverse of the ellipsoid matrix A
62 
63 struct AccelSensor; // actual typedef is located in sensor_fusion_types.h
64 
65 // function prototypes for functions in precisionAcclerometer.c
66 /// Initialize the accelerometer calibration functions
68  struct AccelCalibration *pthisAccelCal, ///< Accelerometer calibration parameter structure
69  struct AccelBuffer *pthisAccelBuffer, ///< Buffer of measurements used as input to the accel calibration functions
70  volatile int8_t *AccelCalPacketOn ///< Used to coordinate calibration sample storage and communications
71 );
72 /// Update the buffer used to store samples used for accelerometer calibration.
74  struct AccelCalibration *pthisAccelCal, ///< Accelerometer calibration parameter structure
75  struct AccelBuffer *pthisAccelBuffer, ///< Buffer of measurements used as input to the accel calibration functions
76  struct AccelSensor* pthisAccel, ///< Pointer to the accelerometer input/state structure
77  volatile int8_t *AccelCalPacketOn ///< Used to coordinate calibration sample storage and communications
78 );
79 /// function maps the accelerometer data fGs (g) onto precision calibrated and de-rotated data fGc (g), iGc (counts)
80 void fInvertAccelCal(
81  struct AccelSensor *pthisAccel, ///< Pointer to the accelerometer input/state structure
82  struct AccelCalibration *pthisAccelCal ///< Accelerometer calibration parameter structure
83 );
84 /// function runs the precision accelerometer calibration
86  struct AccelCalibration *pthisAccelCal, ///< Accelerometer calibration parameter structure
87  struct AccelBuffer *pthisAccelBuffer, ///< Buffer of measurements used as input to the accel calibration functions
88  struct AccelSensor* pthisAccel ///< Pointer to the accelerometer input/state structure
89 );
90 /// calculate the 4 element calibration from the available measurements
92  struct AccelBuffer *pthisAccelBuffer, ///< Buffer of measurements used as input to the accel calibration functions
93  struct AccelCalibration *pthisAccelCal, ///< Accelerometer calibration parameter structure
94  struct AccelSensor* pthisAccel ///< Pointer to the accelerometer input/state structure
95 );
96 /// calculate the 7 element calibration from the available measurements
98  struct AccelBuffer *pthisAccelBuffer, ///< Buffer of measurements used as input to the accel calibration functions
99  struct AccelCalibration *pthisAccelCal, ///< Accelerometer calibration parameter structure
100  struct AccelSensor* pthisAccel ///< Pointer to the accelerometer input/state structure
101 );
102 /// calculate the 10 element calibration from the available measurements
104  struct AccelBuffer *pthisAccelBuffer, ///< Buffer of measurements used as input to the accel calibration functions
105  struct AccelCalibration *pthisAccelCal, ///< Accelerometer calibration parameter structure
106  struct AccelSensor* pthisAccel ///< Pointer to the accelerometer input/state structure
107 );
108 
109 #endif // PRECISIONACCELEROMETER_H
int16_t iStoreLocation
-1 for none, 0 to 11 for the 12 storage locations
int16_t iStoreCounter
number of remaining iterations at FUSION_HZ to average measurement
void fRunAccelCalibration(struct AccelCalibration *pthisAccelCal, struct AccelBuffer *pthisAccelBuffer, struct AccelSensor *pthisAccel)
function runs the precision accelerometer calibration
void fUpdateAccelBuffer(struct AccelCalibration *pthisAccelCal, struct AccelBuffer *pthisAccelBuffer, struct AccelSensor *pthisAccel, volatile int8_t *AccelCalPacketOn)
Update the buffer used to store samples used for accelerometer calibration.
float fGsStored[MAX_ACCEL_CAL_ORIENTATIONS][3]
uncalibrated accelerometer measurements (g)
int16_t iStoreFlags
denotes which measurements are present
The AccelSensor structure stores raw and processed measurements for a 3-axis accelerometer.
void fComputeAccelCalibration4(struct AccelBuffer *pthisAccelBuffer, struct AccelCalibration *pthisAccelCal, struct AccelSensor *pthisAccel)
calculate the 4 element calibration from the available measurements
struct AccelCalibration AccelCalibration
precision accelerometer calibration structure
void fComputeAccelCalibration7(struct AccelBuffer *pthisAccelBuffer, struct AccelCalibration *pthisAccelCal, struct AccelSensor *pthisAccel)
calculate the 7 element calibration from the available measurements
accelerometer measurement buffer
#define MAX_ACCEL_CAL_ORIENTATIONS
number of stored precision accelerometer measurements
void fInvertAccelCal(struct AccelSensor *pthisAccel, struct AccelCalibration *pthisAccelCal)
function maps the accelerometer data fGs (g) onto precision calibrated and de-rotated data fGc (g)...
void fInitializeAccelCalibration(struct AccelCalibration *pthisAccelCal, struct AccelBuffer *pthisAccelBuffer, volatile int8_t *AccelCalPacketOn)
Initialize the accelerometer calibration functions.
float fSumGs[3]
averaging sum for current storage location
void fComputeAccelCalibration10(struct AccelBuffer *pthisAccelBuffer, struct AccelCalibration *pthisAccelCal, struct AccelSensor *pthisAccel)
calculate the 10 element calibration from the available measurements
struct AccelBuffer AccelBuffer
accelerometer measurement buffer
precision accelerometer calibration structure